package com.systech.core.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.apache.log4j.Logger;

public class PropertiesFile {

	private static Properties properties = null;
	
	private static Logger logger = Logger.getLogger(PropertiesFile.class);

	private PropertiesFile() {

	}

	public static void load(String ruta) {

		FileInputStream stream = null;
		try {
			File archivo = new File(ruta);

			properties = new Properties();
			stream = new FileInputStream(archivo);
			properties.load(stream);
			stream.close();
		}
		catch (FileNotFoundException e) {
			logger.error("No se encontro archivo de propieades " + ruta);
		}
		catch (IOException e) {
			logger.error("Error I/O en el archivo de propiedades " + ruta);
		}

	}

	public static String getProperty(String name) {

		if (properties != null) {
			return properties.getProperty(name);
		}
		return null;

	}

	public static Number getPropertyNumber(String name) {

		String str = PropertiesFile.getProperty(name);
		Number dimension = null;
		if (StringUtils.isNotEmpty(str) && NumberUtils.isNumber(str)) {
			dimension = NumberUtils.createNumber(str);
		}
		return dimension;

	}

	public static boolean getPropertyBoolean(String name) {

		if (properties != null) {
			String str =  properties.getProperty(name);
			if (str.toLowerCase().startsWith("s") || str.toLowerCase().startsWith("y") || str.toLowerCase().startsWith("t")) {
				return true;
			}
		}
		return false;

	}
}
